/**
 * 修改密码
 * @return
 */
function saveUserPass(){
	//1.获取参数
	var id = $("#id").val();
	var password = $.trim($("#password").val());
	var newPassword = $.trim($("#newPassword").val());
	var confirmPassword = $.trim($("#confirmPassword").val());
	
	//2.验证参数
	
	//3.发送请求
	var url = ctx + "/saveUserPass.html?ajax=true";
	var param = "&id=" + id + "&password=" + password + "&newPassword=" + newPassword + "&confirmPassword=" + confirmPassword;
	var sf = function (msg) {
		if (msg == 1 || msg == "1" || msg == "\"1\"") {
			art.dialog({
			    id: "sucId",
			    icon: "succeed",
			    content: "保存成功！"
			});
			art.dialog({id: "sucId"}).title("2秒后关闭").time(2);
		}else if (msg == 2 || msg == "2" || msg == "\"2\"") {
			art.dialog({
			    id: "warnId",
			    icon: "warning",
			    content: "原密码与当前密码不匹配，请重新输入！"
			});
			art.dialog({id: "warnId"}).title("2秒后关闭").time(2);
		}else{
			art.dialog({
			    id: "errId",
			    icon: "error",
			    content: "保存失败，请刷新页面重试！"
			});
			art.dialog({id: "errId"}).title("2秒后关闭").time(2);
		}
	};
	ajax(url, param, sf);
}


/***
 *	修改个人基本信息 
 */
function saveUserInfo(){
	//1.获取参数
	var id = $("#id").val();
	var displayName = $.trim($("#displayName").val());
	var nickName = $.trim($("#nickName").val());
	var gender = $("#gender").val();
	var year = $("#year").val();
	var month = $("#month").val();
	var date = $("#date").val();
	var province = $("#province").val();
	var city = $("#city").val();
	var county = $("#county").val();
	
	
	//2.验证参数
	
	//3.发送请求
	var url = ctx + "/saveUserInfo.html?ajax=true";
	var param = "&user.id=" + id + "&user.displayName=" + encodeURIComponent(displayName) + "&userInfo.nickName=" + encodeURIComponent(nickName) + "&user.gender=" + gender + "&userInfo.birthYear=" + year + "&userInfo.birthMonth=" + month + "&userInfo.birthDay=" + date + "&userInfo.province=" + encodeURIComponent(province) + "&userInfo.city=" + encodeURIComponent(city) + "&userInfo.county=" + encodeURIComponent(county);
	var sf = function (msg) {
		if (msg == 1 || msg == "1" || msg == "\"1\"") {
			art.dialog({
			    id: "sucId",
			    icon: "succeed",
			    content: "保存成功！"
			});
			art.dialog({id: "sucId"}).title("2秒后关闭").time(2);
		}else{
			art.dialog({
			    id: "errId",
			    icon: "error",
			    content: "保存失败，请刷新页面重试！"
			});
			art.dialog({id: "errId"}).title("2秒后关闭").time(2);
		}
	};
	ajax(url, param, sf);
}


/***
 *	保存位置 
 */
function saveLocation(){
	//1.获取参数
	var id = $("#id").val();
	var location = $("#location").val();
	var address = $.trim($("#address").val());
	var province = $("#province").val();
	var city = $("#city").val();
	var county = $("#county").val();
	
	
	//2.验证参数
	
	//3.发送请求
	var url = ctx + "/saveLocation.html?ajax=true";
	var param = "&user.id=" + id + "&location=" + location + "&userInfo.address=" + encodeURIComponent(address) + "&userInfo.province=" + encodeURIComponent(province) + "&userInfo.city=" + encodeURIComponent(city) + "&userInfo.county=" + encodeURIComponent(county);
	var sf = function (msg) {
		if (msg == 1 || msg == "1" || msg == "\"1\"") {
			art.dialog({
			    id: "sucId",
			    icon: "succeed",
			    content: "保存成功！"
			});
			art.dialog({id: "sucId"}).title("2秒后关闭").time(2);
		}else{
			art.dialog({
			    id: "errId",
			    icon: "error",
			    content: "保存失败，请刷新页面重试！"
			});
			art.dialog({id: "errId"}).title("2秒后关闭").time(2);
		}
	};
	ajax(url, param, sf);	
}

/**
 * 根据企业名称查询企业
 * @return
 */
function searchDept(){
	//1.获取参数
	var deptName = $.trim($("#deptName").val());
	
	//2.验证参数
	if (isUndefined(deptName) || deptName == null || "" == deptName || deptName == "请输入团体或企业名称") {
		art.dialog.tips("<label class='warn'>请输入团体或企业名称！</label>", 1.5);
		return false;
	}
	
	//3.发送请求
	var url = ctx + "/searchDept.html?ajax=true";
	var param = "&deptName=" + deptName;
	var sf = function (msg) {
		$("#company").show();
		$("#company").html(msg);
	};
	ajax(url, param, sf);
}

/**
 * 绑定企业申请
 * @param deptId
 * @param userId
 * @return
 */
function bindDept(deptId,userId,type){
	var url = ctx + "/bindDept.html?ajax=true";
	var param = "&actionType=" + type + "&deptId=" + deptId + "&userId=" + userId;
	var sf = function (msg) {
		if (msg == 1 || msg == "1" || msg == "\"1\"") {
			art.dialog.tips("<label class='success'>申请绑定成功，请等待对方审核！</label>", 1.5);
		}else if (msg == 2 || msg == "2" || msg == "\"2\"") {
			art.dialog.tips("<label class='warn'>您的申请已发送，请耐心等待回应！</label>", 1.5);
		}else if (msg == 3 || msg == "3" || msg == "\"3\"") {
			art.dialog.tips("<label class='warn'>很抱歉，您已经绑定到了企业！</label>", 1.5);
		}else if (msg == 4 || msg == "4" || msg == "\"4\"") {
			art.dialog.tips("<label class='warn'>请注意，该用户已经与本企业解除绑定！</label>", 2);
			window.location.reload();
		}else{
			art.dialog.tips("<label class='error'>操作失败，请刷新页面重试！</label>", 1.5);
		}
	};
	ajax(url, param, sf);
}

/**
 * 申请人员审核
 * @param type
 * @return
 */
function bindUser(deptId,userId,type){
	var url = ctx + "/bindUser.html?ajax=true";
	var param = "&actionType=" + type + "&deptId=" + deptId + "&userId=" + userId;
	var sf = function (msg) {
		if (msg == 1 || msg == "1" || msg == "\"1\"") {
			art.dialog.tips("<label class='success'>申请通过，欢迎新成员加入！</label>", 2);
			window.location.reload();
		}else if (msg == 2 || msg == "2" || msg == "\"2\"") {
			art.dialog.tips("<label class='warn'>很遗憾，申请没有通过！</label>", 2);
			window.location.reload();
		}else{
			art.dialog.tips("<label class='error'>操作失败，请刷新页面重试！</label>", 1.5);
		}
	};
	ajax(url, param, sf);
}

function searchGroup(){
	art.dialog.open(
		ctx + "/searchGroup.html", 
		{
			title: "添加分组",
			width:"450px", 
			height:"200px", 
			button:[
		        {
		        	name: "保存分组",
		            callback: function () {
		        		if(isUndefined(saveGroup())){
			        		$("button").attr("disabled", true);
		        			this.close();
		        		}	
		                return false;
		            },
		            focus: true
		        },
		        {
		        	name: "取消"
		        }
			],
			lock: true
		}
	);
}

function saveGroup(){
	//1.获取参数
	var deptName = $.trim($(window.frames[0].document).find("#deptName").val());
	var userId = window.frames[0].$("input[name='userId']:checked").val();
	
	//2.验证参数
	if (isUndefined(deptName) || deptName == null || "" == deptName) {
		art.dialog.tips("<label class='warn'>请输入分组名称！</label>", 1.5);
		return false;
	}
	
	//3.发送请求
	var url = ctx + "/saveGroup.html?ajax=true";
	var param = "&deptName=" + encodeURIComponent(deptName);
	if (!isUndefined(userId) && userId != null && "" != userId) {
		param += "&userId=" + userId;
	}
	var sf = function (msg) {
		if (msg == 1 || msg == "1" || msg == "\"1\"") {
			art.dialog.tips("<label class='success'>保存成功！</label>", 2);
			window.location.reload();
		}else if (msg == 2 || msg == "2" || msg == "\"2\"") {
			art.dialog.tips("<label class='warn'>很抱歉，您添加的分组名称已存在，请尝试其他的名称！</label>", 2);
			window.location.reload();
		}else{
			art.dialog.tips("<label class='error'>保存失败，请刷新页面重试！</label>", 1.5);
		}
	};
	ajax(url, param, sf);
}

function searchGroupUser(deptId){
	$("a").css("background-color","");
	$("#group_" + deptId).css("background-color","#eff2f7");
	var url = ctx + "/searchGroupUser.html?ajax=true";
	var param = "&deptId=" + deptId;
	var sf = function (msg) {
		$("#userList").html(msg);
	};
	ajax(url, param, sf);
}

function changeGroup(userId,deptId){
	var url = ctx + "/changeGroup.html?ajax=true";
	var param = "&userId=" + userId + "&deptId=" + deptId;
	var sf = function (msg) {
		if (msg == 1 || msg == "1" || msg == "\"1\"") {
			art.dialog.tips("<label class='success'>操作成功！</label>", 2);
			searchGroupUser(deptId);
			$("a").css("background-color","");
			$("#group_" + deptId).css("background-color","#eff2f7");
		}else if (msg == 2 || msg == "2" || msg == "\"2\"") {
			art.dialog.tips("<label class='warn'>您选择的分组和您的当前分组一致，请重新选择！</label>", 2);
		}else{
			art.dialog.tips("<label class='error'>操作失败，请刷新页面重试！</label>", 1.5);
		}
	};
	ajax(url, param, sf);
}
